<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@page import="inet.db.tennis.TennisChampion"%>
<%@page import="inet.db.tennis.TennisCategory"%>
<%@page import="inet.db.tennis.TennisCountry"%>
<%@page import="java.math.BigDecimal"%>
<%@page import="inet.util.StringTool"%>
<%@page import="inet.db.tennis.TennisSeason"%>
<%@page import="inet.db.tennis.TennisPhase"%>
<%@page import="inet.db.tennis.TennisRound"%>
<%@page import="inet.db.tennis.TennisConst"%>
<%@page import="inet.db.tennis.buffer.TennisBuffer"%>
<jsp:useBean id="tennisCategoriesDAO" class="inet.db.tennis.TennisCategoryDAO" scope="session"/>
<jsp:useBean id="tennisCountryDAO" class="inet.db.tennis.TennisCountryDAO" scope="session"/>
<jsp:useBean id="tennisChampionDAO" class="inet.db.tennis.TennisChampionDAO" scope="session"/>
<jsp:useBean id="tennisSeasonDAO" class="inet.db.tennis.TennisSeasonDAO" scope="session"/>
<jsp:useBean id="tennisPhaseDAO" class="inet.db.tennis.TennisPhaseDAO" scope="session"/>
<jsp:useBean id="tennisRoundDAO" class="inet.db.tennis.TennisRoundDAO" scope="session"/>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8" />
    <title>Sửa thông tin giải đấu</title>
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/reset.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/text.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/grid.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/layout.css" media="screen" />
    <link rel="stylesheet" type="text/css" href="<%= request.getContextPath() %>/admin/tennis/css/nav.css" media="screen" />
    <!--[if IE 6]><link rel="stylesheet" type="text/css" href="css/ie6.css" media="screen" /><![endif]-->
    <!--[if IE 7]><link rel="stylesheet" type="text/css" href="css/ie.css" media="screen" /><![endif]-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/fancy-button/fancy-button.css" rel="stylesheet" type="text/css" />
    <!--Jquery UI CSS-->
    <link href="<%= request.getContextPath() %>/admin/tennis/css/themes/base/jquery.ui.all.css" rel="stylesheet" type="text/css" />
    <!-- BEGIN: load jquery -->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-1.6.4.min.js" type="text/javascript"></script>
    <script type="text/javascript" src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.core.min.js"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.widget.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.ui.accordion.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.core.min.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/jquery-ui/jquery.effects.slide.min.js" type="text/javascript"></script>
    <!-- END: load jquery -->
    
    <!--Fancy Button-->
    <script src="<%= request.getContextPath() %>/admin/tennis/js/fancy-button/fancy-button.js" type="text/javascript"></script>
    <script src="<%= request.getContextPath() %>/admin/tennis/js/setup.js" type="text/javascript"></script>
    <script type="text/javascript">
    	function f_edit_champion() {
    		if(document.formEditChampion.txtName.value==''){
				document.getElementById("lblName").innerHTML = 'Nhập tên giải đấu';
				document.formEditChampion.txtName.focus();
				return false;
			}else{
				document.getElementById("lblName").innerHTML = '';
			}

    		if(document.formEditChampion.txtCode.value==''){
				document.getElementById("lblCode").innerHTML = 'Nhập mã giải đấu';
				document.formEditChampion.txtCode.focus();
				return false;
			}else{
				document.getElementById("lblCode").innerHTML = '';
			}
			
    		if(document.formEditChampion.slbSeason.value=='0'){
				document.getElementById("lblSeason").innerHTML = 'Chọn mùa giải cho giải đấu';
				document.formEditChampion.slbSeason.focus();
				return false;
			}else{
				document.getElementById("lblSeason").innerHTML = '';
			}
			
    		if(document.formEditChampion.slbPhaseId.value=='0'){
				document.getElementById("lblPhase").innerHTML = 'Chọn giai đoạn cho giải đấu';
				document.formEditChampion.slbPhaseId.focus();
				return false;
			}else{
				document.getElementById("lblPhase").innerHTML = '';
			}
			
    		if(document.formEditChampion.slbRoundId.value=='0'){
				document.getElementById("lblRound").innerHTML = 'Chọn vòng đấu cho giải đấu';
				document.formEditChampion.slbRoundId.focus();
				return false;
			}else{
				document.getElementById("lblRound").innerHTML = '';
			}
			
    		document.formEditChampion.action.value = "UPDATE";
    		document.formEditChampion.submit();
    	}
    	function disableEnterKey(e)
		{
		     var key;
		     if(window.event)
		          key = window.event.keyCode;//IE
		     else
		          key = e.which;//firefox
		     if(key == 13)
		        f_edit_champion();
		}
		function loadPhase(url, function_change, seasonId, championCode, phaseId) {
			url = url + "?championCode="+championCode+"&seasonId="+seasonId+"&phaseId="+phaseId;
		    if (window.XMLHttpRequest) { // Non-IE browsers
		      req = new XMLHttpRequest();
		      req.onreadystatechange = function_change;
		      try {
		          req.open("GET", url, true); //was get
		      } catch (e) {
		          //alert("Problem Communicating with Server\n"+e);
		      }
		      req.send(null);
		    } else if (window.ActiveXObject) { // IE
			    req = new ActiveXObject("Microsoft.XMLHTTP");
			    if (req) {
			        req.onreadystatechange = function_change;
			        req.open("GET", url, true);
			        req.send();
			    }
		    }
		 }
		function loadRound(url, function_change, phaseId) {
			url = url + "?phaseId="+phaseId;
		    if (window.XMLHttpRequest) { // Non-IE browsers
		      req = new XMLHttpRequest();
		      req.onreadystatechange = function_change;
		      try {
		          req.open("GET", url, true); //was get
		      } catch (e) {
		          //alert("Problem Communicating with Server\n"+e);
		      }
		      req.send(null);
		    } else if (window.ActiveXObject) { // IE
			    req = new ActiveXObject("Microsoft.XMLHTTP");
			    if (req) {
			        req.onreadystatechange = function_change;
			        req.open("GET", url, true);
			        req.send();
			    }
		    }
		 }
		 function processPhaseLoadData() {
			  //alert(req.readyState);
		      if (req.readyState == 4) { // Complete
			      if (req.status == 200) { // OK response
			      	document.getElementById("divPhase").innerHTML = req.responseText;
				  } else {
			        //alert("Problem with server response:\n " + req.statusText);
			      }
		     }else{
		     	//
		     }
	 	}
		 function processRoundLoadData() {
			  //alert(req.readyState);
		      if (req.readyState == 4) { // Complete
			      if (req.status == 200) { // OK response
			      	document.getElementById("divRound").innerHTML = req.responseText;
				  } else {
			        //alert("Problem with server response:\n " + req.statusText);
			      }
		     }else{
		     	//
		     }
	 	}
    </script>
</head>
<body>
    <div class="container_12">
        <%@ include file="../include/header.jsp" %>
        <% 
        	// set Character Encoding before any getParameter() methods.
		    request.setCharacterEncoding("UTF-8");
		    response.setCharacterEncoding("UTF-8");
		    String sId = request.getParameter("id");
		    if(sId == null || !StringTool.isNumberic(sId)) {
		    	sId = "0";
		    }
		    String sAction = request.getParameter("action");
		    String sName = request.getParameter("txtName");
		    String sCode = request.getParameter("txtCode");
		    String sCateId = request.getParameter("slbCateId");
		    String sCourtType = request.getParameter("slbCourtType");
		    String sCountryId = request.getParameter("slbCountryId");
		    String sTotalSet = request.getParameter("slbTotalSet");
		    String sSeasonId = request.getParameter("slbSeason");
		    String sPhaseId = request.getParameter("slbPhaseId");
		    String sRoundId = request.getParameter("slbRoundId");
		    String sLevel = request.getParameter("slbLevel");
		    String sStatus = request.getParameter("slbStatus");
		    String sPoint = request.getParameter("txtPoint");
		    String sBonusMoney = request.getParameter("txtBonusMoney");
		    String sBracket = request.getParameter("rdoBracket");
		    String sOldChamionCode = request.getParameter("championCodeHid");
		    
		    String sMessageErr = "";
		    String sMessageOk = "";
		    String sMessageWarning = "";
		    if(sAction != null && "UPDATE".equals(sAction)) {
		    	if(sName == null || "".equals(sName) ) {
		    		sMessageWarning = "Bạn phải nhập tên giải đấu";
		    	} else if(sCode == null || "".equals(sCode) ) {
		    		sMessageWarning = "Bạn phải nhập mã giải đấu";
		    	} else if(sSeasonId == null || "0".equals(sSeasonId) ) {
		    		sMessageWarning = "Bạn phải nhập mùa giải hiện tại của giải đấu";
		    	} else if(sPhaseId == null || "0".equals(sPhaseId) ) {
		    		sMessageWarning = "Bạn phải nhập giai đoạn hiện tại của giải đấu";
		    	} else if(sRoundId == null || "".equals(sRoundId) ) {
		    		sMessageWarning = "Bạn phải nhập vòng đấu hiện tại giải đấu";
		    	} else {
		    		boolean isExistedChampionCode = false;
		    		// Neu thay doi ma giai dau
		    		if(!sOldChamionCode.equalsIgnoreCase(sCode) ) {
		    			isExistedChampionCode = tennisChampionDAO.isExistedChampionCode(sCode);
		    		}
		    		if(!isExistedChampionCode) {
			    		boolean isOk = tennisChampionDAO.updateRow(new BigDecimal(sId), sCode, sName, new BigDecimal(sCateId), Integer.parseInt(sCourtType),
			    			 new BigDecimal(sCountryId), Integer.parseInt(sTotalSet), new BigDecimal(sPhaseId), sRoundId, new BigDecimal(sSeasonId), 
			    			 Integer.parseInt(sStatus), Integer.parseInt(sLevel), Integer.parseInt(sPoint), sBonusMoney, Integer.parseInt(sBracket));
			    		if(isOk) {
			    			TennisBuffer.load();
			    			sMessageOk = "Sửa thông tin giải đấu thành công";
			    		} else {
			    			sMessageErr = "Xảy ra lỗi khi sửa thông tin giải đấu";
			    		}
		    		} else {
		    			sMessageWarning = "Mã giải đấu đã tồn tại";
		    		}
		    	}
		    }
		    TennisChampion champion = tennisChampionDAO.getRow(new BigDecimal(sId));
        %>
        <div class="grid_5">
            <div class="box round first fullpage">
                <h2>Sửa thông tin giải đấu</h2>
                <div class="block ">
                	<% if(champion != null) { %>
                    <form name="formEditChampion" onKeyPress="return disableEnterKey(event)" method="post">
                    	<input type="hidden" name="action"/>
                    	<input type="hidden" name="id" value="<%= sId %>"/>
                    	<input type="hidden" name="championCodeHid" value="<%= champion.getCode() %>"/>
                    	<% if( !"".equals(sMessageWarning) ) { %>
                    	<div class="message warning">
                            <h5>Cảnh báo !</h5>
                            <p>
                                <%= sMessageWarning %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageOk) ) { %>
                    	<div class="message success">
                            <h5>Success!</h5>
                            <p>
                                <%= sMessageOk %>
                            </p>
                        </div>
                        <% } %>
                    	<% if( !"".equals(sMessageErr) ) { %>
                    	<div class="message error">
                            <h5>Error!</h5>
                            <p>
                                <%= sMessageErr %>
                            </p>
                        </div>
                        <% } %>
	                    <table class="form">
	                    	<tr>
	                            <td>
	                                <label>Mùa giải</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbSeason"
	                                	onchange="loadPhase('<%= request.getContextPath() %>/admin/tennis/process/load-phase-of-season.jsp',processPhaseLoadData,document.formEditChampion.slbSeason.value,'<%= champion.getCode() %>', '<%= champion.getCurrentPhase() %>');">
	                                	<option value="0"> -Chọn mùa giải- </option>
	                                	<% 
	                                		Vector<TennisSeason> vSeason = tennisSeasonDAO.findAll(champion.getCode());
	                                		if(vSeason != null && !vSeason.isEmpty()) {
	                                			TennisSeason season = null;
	                                			for (Iterator it = vSeason.iterator(); it.hasNext(); ) {
	                                				season = (TennisSeason) it.next();
	                                				if(season == null) continue;
	                                	%>
	                                    <option <%= season.getId().equals(champion.getCurrentSeason()) ? "selected":"" %> value="<%= season.getId() %>"><%= season.getSeasonName() %></option>
	                                    <%  
	                                    		}
	                                    	}
	                                    %>
	                                </select>
	                                <span id="lblSeason" class="error"></span>
	                            </td>
	                        </tr>
	                    	<tr>
	                            <td>
	                                <label>Giai đoạn</label>
	                            </td>
	                            <td>
	                                <div id="divPhase">
	                                	<select id="select" name="slbPhaseId">
	                                	<% 
	                                		if (champion.getCurrentSeason() != null ) {
		                                		Vector<TennisPhase> vPhase = tennisPhaseDAO.findAll(champion.getCode(), champion.getCurrentSeason());
												if(vPhase != null && !vPhase.isEmpty()) {
													TennisPhase phase = null;
													for (Iterator it = vPhase.iterator(); it.hasNext(); ){
														phase = (TennisPhase) it.next();
														if(phase== null) continue;
										%>
											<option <%= (phase.getId().equals(champion.getCurrentPhase()) ? "selected":"") %> value="<%= phase.getId() %>"><%=phase.getPhaseName() %></option>
										<%
													}
												}
											}
	                                	%>
	                                	</select>
	                                </div>
	                                <span id="lblPhase" class="error"></span>
	                            </td>
	                        </tr>
	                    	<tr>
	                            <td>
	                                <label>Vòng đấu</label>
	                            </td>
	                            <td>
	                                <div id="divRound">
	                                	<select id="select" name="slbRoundId">
	                                	<% 
	                                		if (champion.getCurrentPhase() != null ) {
		                                		Vector<TennisRound> vRound = tennisRoundDAO.findAll(champion.getCurrentPhase());
												if(vRound != null && !vRound.isEmpty()) {
													TennisRound round = null;
													for (Iterator it = vRound.iterator(); it.hasNext(); ){
														round = (TennisRound) it.next();
														if(round== null) continue;
										%>
											<option <%= (round.getRoundCode().equals(champion.getCurrentRound()) ? "selected":"") %> value="<%= round.getRoundCode() %>"><%=round.getRoundCode() %></option>
										<%
													}
												}
											}
	                                	%>
	                                	</select>
	                                </div>
	                                <span id="lblRound" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td class="col1">
	                                <label>Tên giải đấu</label>
	                            </td>
	                            <td class="col2">
	                                <input type="text" id="grumble" name="txtName" value="<%= champion.getName() %>"/>
	                                <span id="lblName" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Mã giải đấu</label>
	                            </td>
	                            <td>
	                                <input type="text" id="grumble" name="txtCode" value="<%= champion.getCodeSMS() != null ? champion.getCodeSMS():"" %>"/>
	                                <span id="lblCode" class="error"></span>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Thể loại</label>
	                            </td>
	                            <td>
	                            	<% 
	                            		Vector<TennisCategory> vCategories = tennisCategoriesDAO.findAll();
	                            	%>
	                                <select id="select" name="slbCateId">
	                                	<% 
	                                		if(vCategories != null && !vCategories.isEmpty()) {
	                                			TennisCategory category = null;
	                                			for (Iterator it = vCategories.iterator(); it.hasNext(); ) {
	                                				category = (TennisCategory) it.next();
	                                				if(category == null) continue;
	                                	%>
	                                    <option <%= category.getId().equals(champion.getCateId()) ? "selected":"" %> 
	                                    	value="<%= category.getId() %>"><%= category.getName() %></option>
	                                    <% 
	                                    		}
	                                    	} 
	                                    %>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Loại sân</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbCourtType">
	                                    <option <%= champion.getCourtType() == 0 ? "selected":"" %> value="0">Hand</option>
	                                    <option <%= champion.getCourtType() == 1 ? "selected":"" %> value="1">Clay</option>
	                                    <option <%= champion.getCourtType() == 2 ? "selected":"" %> value="2">Grass</option>
	                                    <option <%= champion.getCourtType() == 3 ? "selected":"" %> value="3">Omni</option>
	                                    <option <%= champion.getCourtType() == 4 ? "selected":"" %> value="4">Carpet</option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Quốc gia</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbCountryId">
	                                	<%
	                                		Vector<TennisCountry> vCountry = tennisCountryDAO.findAll();
	                                		if(vCountry != null && !vCountry.isEmpty()) {
	                                			TennisCountry country = null;
	                                			for (Iterator it = vCountry.iterator(); it.hasNext(); ) {
	                                				country = (TennisCountry) it.next();
	                                				if(country == null) continue;
	                                	%>
	                                    <option <%= country.getId().equals(champion.getCountryId()) ? "selected":"" %> value="<%= country.getId() %>"><%= country.getCountryName() %></option>
	                                    <% 
	                                    		}
	                                    	} 
	                                    %>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Tổng số SET</label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbTotalSet">
	                                    <option <%= champion.getTotalSet() == 3 ? "selected":"" %> value="3">3 SET</option>
	                                    <option <%= champion.getTotalSet() == 5 ? "selected":"" %> value="5">5 SET</option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Hạng </label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbLevel">
	                                    <option <%= champion.getLevel() == 1 ? "selected":"" %> value="1">Hạng 1</option>
	                                    <option <%= champion.getLevel() == 2 ? "selected":"" %> value="2">Hạng 2</option>
	                                    <option <%= champion.getLevel() == 3 ? "selected":"" %> value="3">Hạng 3</option>
	                                    <option <%= champion.getLevel() == 4 ? "selected":"" %> value="4">Hạng 4</option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Trạng thái </label>
	                            </td>
	                            <td>
	                                <select id="select" name="slbStatus">
	                                    <option <%= champion.getLevel() == TennisConst.TENNIS_CHAMPION_WAITING ? "selected":"" %> value="0">Sắp diễn ra</option>
	                                    <option <%= champion.getLevel() == TennisConst.TENNIS_CHAMPION_RUNNING ? "selected":"" %> value="0">Đang diễn ra</option>
	                                    <option <%= champion.getLevel() == TennisConst.TENNIS_CHAMPION_FINAL ? "selected":"" %> value="0">Đã kết thúc</option>
	                                    <option <%= champion.getLevel() == TennisConst.TENNIS_CHAMPION_STOP ? "selected":"" %> value="0">Không sử dụng</option>
	                                </select>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Có lưu đồ</label>
	                            </td>
	                            <td>
	                                <input type="radio" <%= champion.getHasBracket() == 1 ? "checked":"" %> name="rdoBracket" value="1" />
	                                Có
	                                <input type="radio" <%= champion.getHasBracket() == 0 ? "checked":"" %> name="rdoBracket" value="0" />
	                                Không
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Điểm</label>
	                            </td>
	                            <td>
	                                <input type="text" id="grumble" name="txtPoint" value="<%= champion.getPoint() %>"/>
	                            </td>
	                        </tr>
	                        <tr>
	                            <td>
	                                <label>Tiền thưởng</label>
	                            </td>
	                            <td>
	                                <input type="text" id="grumble" name="txtBonusMoney" value="<%= champion.getBonusMoney() != null ? champion.getBonusMoney():"" %>"/>
	                            </td>
	                        </tr>
	                        <tr>
	                        	<td>
	                                <label>&nbsp;</label>
	                            </td>
	                            <td>
	                            	<button type="button" onclick="f_edit_champion();" class="btn btn-grey">Cập nhật</button>
	                            	<button type="button" onclick="window.location.href='index.jsp';" class="btn btn-grey">Quay lại</button>
	                            </td>
	                        </tr>
	                    </table>
                    </form>
                    <% } else { %>
                    <div class="message warning">
                        <h5>Cảnh báo !</h5>
                        <p>
                            Không tồn tại giải đấu có Id = <%= sId %>
                        </p>
                    </div>
                    <% } %>
                </div>
            </div>
        </div>
        <div class="clear">
        </div>
    </div>
    <%@ include file="../include/footer.jsp" %>
</body>
</html>